package com.day0922.json;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import javax.xml.crypto.dom.DOMCryptoContext;
import java.util.Arrays;

public class Text {
    public static void main(String[] args) {
        //字符串 可以表示 类，数组
        //字符串和 类 或数组之间的一个类型转换
        //第一种方式，使用第三提供jar
//        Student student = new Student();
//        student.setName("张三");
//        student.setAge(18);
//        String str = JSON.toJSONString(student);
//        System.out.println(str);
//        String string = "[{\"name\":\"Amy\",\"age\":19,\"sex\":\"female\"},{\"name\":\"Arran\",\"age\":18,\"sex\":\"female\"},\n" +
//                "        {\"name\":\"Jackson\",\"age\":21,\"sex\":\"male\"}]";
//        JSONArray jsonArray = JSONArray.parseArray(string);

        //面向对象三大特征   封装  继承  多态
        //封装是一种思想
        //  get  set  封装中的一种
//        Teacher teacher = new Teacher();
//        teacher.setName("张三");
//        teacher.setAge(1125);
//        teacher.show();

//        Dog dog = new Dog("旺财",18,"柴犬");
//        //  json格式：{name:"旺财",age:18,strain:"柴犬"}
////        把Java对象转换成json字符串
//        String str = JSON.toJSONString(dog);
//        System.out.println(str);
//
////        把json字符串转换成Java对象
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        System.out.println(jsonObject.get("name"));
//        Dog dog1 = JSONObject.parseObject(str,dog.getClass());
//        System.out.println(dog1.getName());
//        System.out.println(dog1.getStrain());

        int[] arrint = new int[5];
        Dog dog1 = new Dog("旺财",5,"柴犬");
        Dog dog2 = new Dog("花花",2,"边牧");
        Dog[] dogs = new Dog[2];
        dogs[0] = dog1;
        dogs[1] = dog2;
        String str = JSON.toJSONString(dogs);//对象数组转换成字符串
        //System.out.println(Arrays.toString(dogs));
        System.out.println(str);
        JSONArray jsonArray = JSONArray.parseArray(str);
        Dog[] dogs1 = jsonArray.toArray(Dog.class);
        System.out.println(dogs1[1].getName());

    }
}

